package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.GoodsBeen;
import cn.xfdzx.android.apps.shop.bean.SelectImageBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ItemLengthListener;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.activity.myshop.SelectImageActivity.GOOD_IS_SELECT;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class SelectGoodsImageAdapter extends RecyclerView.Adapter<SelectGoodsImageAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private SelectImageBeen.DataBean dataBean;
    private Context mContext;
    private String type;
    private int one_position;
    private  ItemLengthListener listener;
    private ArrayList<String> imageUrl;


    public SelectGoodsImageAdapter(Context mContext, SelectImageBeen.DataBean dataBean, String type, int one_position, ItemLengthListener listener,ArrayList<String> imageUrl) {
        this.dataBean = dataBean;
        this.mContext = mContext;
        this.type = type;
        this.one_position = one_position;
        this.listener = listener;
        this.imageUrl = imageUrl;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_goods_image, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(dataBean.getPic().get(position)).apply(options).into(holder.ivGood);
        if (imageUrl.contains(dataBean.getPic().get(position))) {
            if (type.equals("0")) {
                holder.ivSelectImage.setImageResource(R.drawable.icon_select_orange);
            }else {
                holder.ivSelectImage.setImageResource(R.drawable.icon_select_green);
            }
        } else {
            holder.ivSelectImage.setImageResource(R.drawable.icon_select_dui_hui);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (GOOD_IS_SELECT == -1||GOOD_IS_SELECT == one_position) {
                    GOOD_IS_SELECT=one_position;
                    if (imageUrl.contains(dataBean.getPic().get(position))) {

                        imageUrl.remove(dataBean.getPic().get(position));
                        if (imageUrl.size()==0){
                            GOOD_IS_SELECT=-1;
                        }
                        listener.onItemClick(  );
                        notifyDataSetChanged();
                    }else {
                        if (imageUrl.size()<5){
                            imageUrl.add(dataBean.getPic().get(position));
                            listener.onItemClick(  );
                            notifyDataSetChanged();
                        }else {
                            Toast.makeText(mContext, "最多可选5张", Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    Toast.makeText(mContext, "只能选择同一种商品下图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    public int getItemCount() {

        return dataBean.getPic().size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_good)
        ImageView ivGood;
        @BindView(R.id.iv_select_image)
        ImageView ivSelectImage;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
